package com.qy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

import com.qy.dao.VoucherInfoDao;

import java.util.List;
import java.util.Map;
import java.sql.Types;

import com.qy.db.DbParameter;

import java.util.HashMap;
import java.util.ArrayList;

import com.qy.db.pool.DBPoolMgr;
import com.qy.bean.Option;
import com.qy.bean.VoucherInfo;
import com.qy.db.BaseDao;
import com.qy.util.StaticData;

/**
* u_voucher_info daoImpl实现类的生成
* @author zhangwenlong
* @date Fri Feb 09 10:42:36 GMT+08:00 2018
*/ 
public class VoucherInfoDaoImpl extends BaseDao  implements VoucherInfoDao {

	@Override
	public boolean createVoucherInfo(VoucherInfo voucherInfo) {
		boolean result = false;
		int id = -1;
		if (voucherInfo.beginAdd()) {
			String sql = "insert into u_voucher_info (id,user_id,voucher_type,status,create_date,use_date,buy_user_id,bind_phone,pay_id) values (?,?,?,?,?,?,?,?,?);";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, voucherInfo.getId()));
			param.put(2, new DbParameter(Types.INTEGER, voucherInfo.getUserId()));
			param.put(3, new DbParameter(Types.INTEGER, voucherInfo.getVoucherType()));
			param.put(4, new DbParameter(Types.INTEGER, voucherInfo.getStatus()));
			param.put(5, new DbParameter(Types.TIMESTAMP, voucherInfo.getCreateDate()));
			param.put(6, new DbParameter(Types.TIMESTAMP, voucherInfo.getUseDate()));
			param.put(7, new DbParameter(Types.INTEGER, voucherInfo.getBuyUserId()));
			param.put(8, new DbParameter(Types.VARCHAR, voucherInfo.getBindPhone()));
			param.put(9, new DbParameter(Types.INTEGER, voucherInfo.getPayId()));
			id = execLastId(sql, param);
			result = id > -1;
			voucherInfo.commitAdd(result);
			voucherInfo.setId(id);
		}
		return result;
	}

	@Override
	public List<VoucherInfo> getVoucherInfoList() {
		String sql = "select * from u_voucher_info;";
		PreparedStatement pstmt = execQuery(sql, null);
		ResultSet rs = null;
		List<VoucherInfo> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<VoucherInfo>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public VoucherInfo getVoucherInfoById(int id) {
		String sql = "select * from u_voucher_info where id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, id));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		VoucherInfo voucherInfo = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					voucherInfo = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return voucherInfo;
	}

	@Override
	public boolean updateVoucherInfo(VoucherInfo voucherInfo) {
		boolean result = false;
		if (voucherInfo.beginUpdate()) {
			String sql = "update u_voucher_info set user_id=?,voucher_type=?,status=?,create_date=?,use_date=?,buy_user_id=?,bind_phone=?,pay_id=? where id=?;";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, voucherInfo.getUserId()));
			param.put(2, new DbParameter(Types.INTEGER, voucherInfo.getVoucherType()));
			param.put(3, new DbParameter(Types.INTEGER, voucherInfo.getStatus()));
			param.put(4, new DbParameter(Types.TIMESTAMP, voucherInfo.getCreateDate()));
			param.put(5, new DbParameter(Types.TIMESTAMP, voucherInfo.getUseDate()));
			param.put(6, new DbParameter(Types.INTEGER, voucherInfo.getBuyUserId()));
			param.put(7, new DbParameter(Types.VARCHAR, voucherInfo.getBindPhone()));
			param.put(8, new DbParameter(Types.INTEGER, voucherInfo.getPayId()));
			param.put(9, new DbParameter(Types.INTEGER, voucherInfo.getId()));
			result = execNoneQuery(sql, param) > -1;
			voucherInfo.commitUpdate(result);
		}
		return result;
	}

	public VoucherInfo resultTo(ResultSet rs) throws SQLException {
		VoucherInfo voucherInfo = new VoucherInfo();
		voucherInfo.setId(rs.getInt("id"));
		voucherInfo.setUserId(rs.getInt("user_id"));
		voucherInfo.setVoucherType(rs.getInt("voucher_type"));
		voucherInfo.setStatus(rs.getInt("status"));
		voucherInfo.setCreateDate(rs.getTimestamp("create_date"));
		voucherInfo.setUseDate(rs.getTimestamp("use_date"));
		voucherInfo.setBuyUserId(rs.getInt("buy_user_id"));
		voucherInfo.setBindPhone(rs.getString("bind_phone"));
		voucherInfo.setPayId(rs.getInt("pay_id"));
		voucherInfo.setOp(Option.None);
		return voucherInfo;
	}

	@Override
	public Connection openConn(){
		return DBPoolMgr.getStrategyConn();
	}
	@Override
	public List<VoucherInfo> getVoucherInfoList(int userId) {
		String sql = "select * from u_voucher_info where user_id=? "
			+ "AND status = "+StaticData.VoucherStatus.NORMAL.getType()+" ;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, userId));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		List<VoucherInfo> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<VoucherInfo>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

}

